<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 现金卡页面
 * @package  htdocs
 * @subpackage  company
 * @author  create by allen.qiang
 */

quanli_require('module/base/company/cardFaceValue.class.php');
quanli_require('module/base/common/quanliCommon.class.php');
quanli_require('module/base/company/cardDetail.class.php');
quanli_require('module/base/system/itemHandler.class.php');
quanli_require('module/base/company/cardMaster.class.php');
quanli_require('module/base/system/itemHandler.class.php');

class pa_card {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1: // 现金卡列表
                    $this->cardList();
                    break;

//                case 2://现金卡详情
//                    $this->cardDetailList();
//                    break;

                case 3://现金卡入库列表
                    $this->cardPutInStore();
                    break;

                case 4: //现金卡导出
                    $this->cardExport();
                    break;

                case 5://现金卡编号查询
                    $this->companyCodeList();
                    break;

                case 6://现金卡调拨
                    $this->cardAllot();
                    break;
                //add by 2014/11/03 sunqiang FUN-BK-0011 现金卡密码查询与重置 start
                case 7:
                    $this->cardReset();
                    break;
                //add by 2014/11/03 sunqiang FUN-BK-0011 现金卡密码查询与重置 end

                //add by sunqiang 现金卡类型管理
                case 8:
                    $this->cardType();
                    break;
                default:
                    backendError();
                    break;
            }

        } else {

            backendError();

        }
    }



    //现金卡类型管理
    private function cardType()
    {
        global $G_PGLOB;

        $G_PGLOB->smarty->display('company/cardType.tpl');
    }


    /**
     *现金卡查询重置页面
     *FUN-BK-0011 - 现金卡密码查询与重置
     * @author sunqiang<allen.qiang@fistforward.com>
     * */
    private function cardReset () {
        global $G_PGLOB;


        $G_PGLOB->smarty->display('company/cardReset.tpl');
    }

    /**
     *现金卡列表页
     */
    private function cardList () {

        global $G_PGLOB;




        //实例化现金卡面值类
        $cardFaceValue = new cardFaceValue();
        $arg_array = array (
            'sortname' => 'face_value',
            'sortorder' => 'asc'
        );
        $card_array = $cardFaceValue->search($arg_array);
        $G_PGLOB->smarty->assign_by_ref('card_array',$card_array);

        //商品筛选器
        $commHandler = new commHandler( $G_PGLOB->DBCA ) ;
        $arg_array = array(
            'sortname' 	=> 'selectID' ,
            'sortorder' => 'DESC' ,
            'page' 		=> 0 ,
            'rp' 		=> 0
        );

        $goods_selector_temp = $commHandler->search($arg_array , 'goods_selector') ;

        $goods_selector = array();
        foreach ( $goods_selector_temp as $key=>$value ) {
            $goods_selector[$key]["selectID"] = $value->selectID;
            $goods_selector[$key]["selectName"] = $value->selectName;
        }

        $G_PGLOB->smarty->assign_by_ref('goods_selector' , json_encode($goods_selector));

        //实例基础设置类
        $itemHandler = new itemHandler( $G_PGLOB->DBCA );

        $args_array = array(

            'itemCategoryID' => 26,
            'useFlg' => 1
        );
 
        //获取现金卡规格
        $card_spec_array = $itemHandler->searchItem( $args_array );

        $G_PGLOB->smarty->assign('card_spec_array', $card_spec_array );

        //获取城市信息
        $city_array = $G_PGLOB->getCache('city');
        $G_PGLOB->smarty->assign_by_ref('city_array', $city_array);


        // add by sunqiang 20150416
        quanli_require('module/base/company/cardType.class.php');
        $cardType = new cardType();
        $cardType_arg = array();
        $cardType_array = $cardType->search( $cardType_arg );
        $G_PGLOB->smarty->assign_by_ref('cardType_array', $cardType_array);


        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(70000));
        $G_PGLOB->smarty->display('company/cardList.tpl');

    }

    //导出现金卡信息
    function cardExport(){

        global $G_PGLOB;

        //检查phpexcel类
        quanli_include('framework/phpExecl/PHPExcel.php');
        quanli_include('framework/phpExecl/PHPExcel/Writer/Excel2007.php');

        //实例excel类
        $objPHPExcel = new PHPExcel();

        /**
         * 设置excel的属性
         */
        //创建人
        $objPHPExcel->getProperties()->setCreator("$G_PGLOB->currentUserName");
        //最后修改人
        $objPHPExcel->getProperties()->setLastModifiedBy("$G_PGLOB->currentUserName");
        //标题
        $objPHPExcel->getProperties()->setTitle("导出现金卡");
        //题目
        $objPHPExcel->getProperties()->setSubject("导出现金卡");
        //描述
        $objPHPExcel->getProperties()->setDescription("导出现金卡");
        /**
         * 设置excel的属性
         */

        /**
         * sheet
         */
        //设置当前的sheet
        $objPHPExcel->setActiveSheetIndex(0);
        //设置sheet的name
        $objPHPExcel->getActiveSheet()->setTitle('导出现金卡');
        /**
         * sheet
         */

        //实例化卡管理类
        $cardDetail = new cardDetail( $G_PGLOB->DBCA ) ;

        //查询现金卡信息
        if( isset($_REQUEST['page']) && is_numeric($_REQUEST['page']) ){
            $_REQUEST['rp'] = 5000;
        }

        $result_data = $cardDetail->searchCardDetail( $_REQUEST ) ;

        $sql = "SELECT card_spec FROM card_master WHERE card_id = ".$_REQUEST['card_id'];
        $cardSpec = $G_PGLOB->DBCA->stmt_query($sql);


        /**
         * 设置标题
         */
        $objPHPExcel->getActiveSheet()->setCellValue('A1', '卡号');
        $objPHPExcel->getActiveSheet()->setCellValue('B1', '拆分后卡号');
        $objPHPExcel->getActiveSheet()->setCellValue('C1', '密码');
        $objPHPExcel->getActiveSheet()->setCellValue('D1', '拆分后密码');
        $objPHPExcel->getActiveSheet()->setCellValue('E1', '面值');
        $objPHPExcel->getActiveSheet()->setCellValue('F1', '规格');
        /**
         * 设置标题
         */

        /**
         * 添加数据
         */
        if ($result_data) {

            $i = 2;

            foreach($result_data as &$Info) {

                $strArray=str_split($Info['card_no'],5);
                $cardNo=join("   ",$strArray);

                $pwd = decryptD($Info['card_pwd']);

                $pwdArray=str_split($pwd,5);
                $password=join("   ",$pwdArray);

                $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $Info['card_no']."\t");
                $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $cardNo."\t");
                $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $pwd . "\t");
                $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $password);
                $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $Info['face_value']);
                $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $cardSpec[0]->card_spec);

                $i++;

            }

        }
        /**
         * 添加数据
         */

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(40);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(40);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(40);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(40);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);


        //文件名称
        $fileName = '导出现金卡_' . date('YmdHis') . '.xlsx';

        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        header("Content-Type: application/force-download");
        header("Content-Disposition:inline;filename=$fileName");
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');

    }

    //代金卡详情列表
    function cardDetailList(){

        global $G_PGLOB;

        //接收生产单号ID参数
        $card_id = isset( $_REQUEST['card_id'] )? $_REQUEST['card_id'] : '';

        $G_PGLOB->smarty->assign( 'card_id' , $card_id );
        $G_PGLOB->smarty->display('company/cardDetailList.tpl');

    }

    //现金卡入库列表
    public function cardPutInStore(){

        global $G_PGLOB;

        if ( !isset($_REQUEST['card_id']) || !is_numeric($_REQUEST['card_id']) ) {
            backendError('参数错误');
        }

        //实例化现金卡类
        $cardMaster = new cardMaster() ;

        //查询现金卡信息
        $arg_array = array(
            'card_id' => $_REQUEST['card_id']
        );
        $card_master_array = $cardMaster->search($arg_array);
        if (!$card_master_array) {
            backendError('现金卡信息没有找到', 404);
        }

        //实例化现金卡详情类
        $cardDetail = new cardDetail();

        //查询现金卡详情
        $card_detail_array = $cardDetail->searchCardDetail($arg_array);

        $G_PGLOB->smarty->assign_by_ref('card_master_array', $card_master_array);
        $G_PGLOB->smarty->assign_by_ref('card_id', $_REQUEST['card_id']);
        $G_PGLOB->smarty->assign_by_ref('card_detail_array', $card_detail_array);

        //入库列表
        $card_put_array = $cardMaster->searchcardWarehouseVoucher($arg_array);

        $G_PGLOB->smarty->assign_by_ref('card_put_array', $card_put_array);

        $G_PGLOB->smarty->display('company/cardPutInStore.tpl');

    }


    /**
     * 查看现金卡编号
     */
    private function companyCodeList(){

        global $G_PGLOB;


        if(!isset($_REQUEST['card_id']) || !is_numeric(trim($_REQUEST['card_id'])) ){
            return array('message'=>'参数错误','code'=> 400);
        }

        $G_PGLOB->smarty->assign('card_id', trim($_REQUEST['card_id']));
        $G_PGLOB->smarty->display('company/companyCodeList.tpl');

    }

    //现金卡调拨
    public function cardAllot(){

        global $G_PGLOB;

        if ( !isset($_REQUEST['card_id']) || !is_numeric($_REQUEST['card_id']) ) {
            backendError('参数错误');
        }

        //实例化现金卡类
        $cardMaster = new cardMaster() ;

        //查询现金卡信息
        $arg_array = array(
            'card_id' => $_REQUEST['card_id']
        );
        $card_master_array = $cardMaster->search($arg_array);
        if (!$card_master_array) {
            backendError('现金卡信息没有找到', 404);
        }

        //实例化现金卡详情类
        $cardDetail = new cardDetail();

        //查询现金卡详情
        $card_detail_array = $cardDetail->searchCardDetail($arg_array);

        $G_PGLOB->smarty->assign_by_ref('card_master_array', $card_master_array);
        $G_PGLOB->smarty->assign_by_ref('card_id', $_REQUEST['card_id']);
        $G_PGLOB->smarty->assign_by_ref('card_detail_array', $card_detail_array);


        //实例基础设置类
        $itemHandler = new itemHandler( $G_PGLOB->DBCA );

        //获取调入办公室人数
        $args_array = array(
            'itemCategoryID' => 29,
            'useFlg' => 1
        );
        $allot_array = $itemHandler->searchItem( $args_array );
        $G_PGLOB->smarty->assign_by_ref('allot_array', $allot_array);

        //调拨列表
        $card_allot_array = $cardMaster->searchCardAllot($arg_array);

        $G_PGLOB->smarty->assign_by_ref('card_allot_array', $card_allot_array);


        $G_PGLOB->smarty->display('company/cardAllot.tpl');

    }



}